package com.sulent.frame.web;

import com.sulent.frame.web.utils.VMInfo;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import sun.net.www.protocol.ftp.FtpURLConnection;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @ClassName SulentWebApplication
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/1/11 17:42
 * @Version 1.0
 */
@Slf4j
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT55S")
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.sulent"})
@SpringBootApplication
public class SulentWebApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SulentWebApplication.class, args);
        logApplicationStartup(run.getEnvironment());
    }


    /**
     * 应用启动输出日志
     * @param env
     */
    private static void logApplicationStartup(Environment env) {
        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(contextPath)) {
            contextPath = "/";
        }
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("The host name could not be determined, using `localhost` as fallback");
        }
        log.info(
                "\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t{}://localhost:{}{}/doc.html\n\t" +
                        "External: \t{}://{}:{}{}\n\t" +
                        "Profile(s): \t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                protocol,
                serverPort,
                contextPath,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                env.getActiveProfiles()
        );
        log.info("Sulent-Web-VM-Info:{}",VMInfo.getVmInfo().totalString());
    }
}
